﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DAL;

namespace BAL
{
    public class BussinessHelpers
    {
        public static DataTable GetCurrentRound()
        {
            DataTable dt = GetCurrentRoundGetData();
            return dt;
        }

        public static DataTable GetCurrentRoundGetData()
        {
            DataTable dt = null;
            using (MainDB __db = new MainDB())
            {
                dt = __db.StoredProcedures.GetCurrentRound();
            }
            return dt;
        }

        public static DataTable BoxDatGiai(int top, int round, int awardId, int imageWidth, int imageHeight, int pageIndex, int pageSize,int pageType)
        {
            string cacheName = Utils.KEY_BOXDOATGIAI(top, round, awardId, imageWidth, imageHeight, pageIndex, pageSize,pageType);
            DataTable dt = CacheController.GetFromCacheDependency<DataTable>(cacheName);
            if (dt == null)
            {
                dt = BoxDatGiaiGetData(top, round, awardId, imageWidth, imageHeight, pageIndex, pageSize,pageType);
                CacheController.SaveToCacheDependency(Const.Data_Timeout, cacheName, dt);
            }

            return dt;
        }

        public static DataTable BoxDatGiaiGetData(int top, int round, int awardId, int imageWidth, int imageHeight, int pageIndex, int pageSize,int pageType)
        {
            DataTable dt = null;
            using (MainDB __db = new MainDB())
            {
                dt = __db.StoredProcedures.BoxDatGia(top, round, awardId, pageIndex, pageSize);
                if (dt != null && dt.Rows.Count > 0)
                {
                    dt = Utils.ReFormatTable(dt, imageWidth, imageHeight,pageType);
                }
            }
            return dt;
        }

        public static DataTable BoxDatGiai_Cont(int top, int round, int awardId, string ImageIds, int imageWidth, int imageHeight, int pageIndex, int pageSize,int pageType)
        {
            string cacheName = Utils.KEY_BOXDOATGIAI_CONT(top, round, awardId, imageWidth, imageHeight, pageIndex, pageSize,pageType);
            DataTable dt = CacheController.GetFromCacheDependency<DataTable>(cacheName);
            if (dt == null)
            {
                dt = BoxDatGiai_Cont_GetData(top, round, awardId, ImageIds, imageWidth, imageHeight, pageIndex, pageSize,pageType);
                CacheController.SaveToCacheDependency(Const.Data_Timeout, cacheName, dt);
            }

            return dt;
        }

        public static DataTable BoxDatGiai_Cont_GetData(int top, int round, int awardId, string ImageIds, int imageWidth, int imageHeight, int pageIndex, int pageSize,int pageType)
        {
            DataTable dt = null;
            using (MainDB __db = new MainDB())
            {
                dt = __db.StoredProcedures.BoxDatGia_Cont(top, round, awardId, ImageIds, pageIndex, pageSize);
                if (dt != null && dt.Rows.Count > 0)
                {
                    dt = Utils.ReFormatTable(dt, imageWidth, imageHeight,pageType);
                }
            }
            return dt;
        }

        public static DataTable BoxMoiNhat(int top, int round, int awardId, int imageWidth, int imageHeight, int pageIndex, int pageSize,int pageType)
        {
            string cacheName = Utils.KEY_BOXMOINHAT(top, round, awardId, imageWidth, imageHeight, pageIndex, pageSize,pageType);
            DataTable dt = CacheController.GetFromCacheDependency<DataTable>(cacheName);
            if (dt == null)
            {
                dt = BoxMoiNhatGetData(top, round, awardId, imageWidth, imageHeight, pageIndex, pageSize,pageType);
                CacheController.SaveToCacheDependency(Const.Data_Timeout, cacheName, dt);
            }

            return dt;
        }

        public static DataTable BoxMoiNhatGetData(int top, int round, int awardId, int imageWidth, int imageHeight, int pageIndex, int pageSize,int pageType)
        {
            DataTable dt = null;
            using (MainDB __db = new MainDB())
            {
                dt = __db.StoredProcedures.BoxMoiNhat(top, round, awardId, pageIndex, pageSize);
                if (dt != null && dt.Rows.Count > 0)
                {
                    dt = Utils.ReFormatTable(dt, imageWidth, imageHeight,pageType);
                }
            }
            return dt;
        }

        public static DataTable BoxMoiNhat_Cont(int top, int round, int awardId, string ImageIds, int imageWidth, int imageHeight, int pageIndex, int pageSize,int pageType)
        {
            string cacheName = Utils.KEY_BOXMOINHAT_CONT(top, round, awardId, imageWidth, imageHeight, pageIndex, pageSize,pageType);
            DataTable dt = CacheController.GetFromCacheDependency<DataTable>(cacheName);
            if (dt == null)
            {
                dt = BoxMoiNhat_Cont_GetData(top, round, awardId, ImageIds, imageWidth, imageHeight, pageIndex, pageSize,pageType);
                CacheController.SaveToCacheDependency(Const.Data_Timeout, cacheName, dt);
            }

            return dt;
        }

        public static DataTable BoxMoiNhat_Cont_GetData(int top, int round, int awardId, string ImageIds, int imageWidth, int imageHeight, int pageIndex, int pageSize,int pageType)
        {
            DataTable dt = null;
            using (MainDB __db = new MainDB())
            {
                dt = __db.StoredProcedures.BoxMoiNhat_Cont(top, round, awardId, ImageIds, pageIndex, pageSize);
                if (dt != null && dt.Rows.Count > 0)
                {
                    dt = Utils.ReFormatTable(dt, imageWidth, imageHeight,pageType);
                }
            }
            return dt;
        }

        public static DataTable Search(string keySearch, int round, int awardId, int imageWidth, int imageHeight, int pageIndex, int pageSize,int pageType)
        {
            string cacheName = Utils.KEY_SEARCH(keySearch, round, awardId, imageWidth, imageHeight, pageIndex, pageSize,pageType);
            DataTable dt = CacheController.GetFromCacheDependency<DataTable>(cacheName);
            if (dt == null)
            {
                dt = SearchGetData(keySearch, round, awardId, imageWidth, imageHeight, pageIndex, pageSize,pageType);
                CacheController.SaveToCacheDependency(Const.Data_Timeout, cacheName, dt);
            }

            return dt;
        }

        public static DataTable SearchGetData(string keySearch, int round, int awardId, int imageWidth, int imageHeight, int pageIndex, int pageSize,int pageType)
        {
            DataTable dt = null;
            using (MainDB __db = new MainDB())
            {
                dt = __db.StoredProcedures.Search(keySearch, round, awardId, pageIndex, pageSize);
                if (dt != null && dt.Rows.Count > 0)
                {
                    dt = Utils.ReFormatTable(dt, imageWidth, imageHeight,pageType);
                }
            }
            return dt;
        }

        public static int SearchPageNum(string keySearch, int round, int awardId,int pageSize)
        {
            int PageNum = 0;
            string cacheName = Utils.KEY_SEARCH_PAGE_NUM(keySearch, round, awardId,pageSize);
            DataTable dt = CacheController.GetFromCacheDependency<DataTable>(cacheName);
            if (PageNum == default(int))
            {
                PageNum = SearchPageNumGetData(keySearch, round, awardId, pageSize);
                CacheController.SaveToCacheDependency(Const.Data_Timeout, cacheName, PageNum);
            }
                
            return PageNum;
        }

        public static int SearchPageNumGetData(string keySearch, int round, int awardId, int pageSize)
        {
            string strCount = "0";
            int pageNum;
            int count = 0;
            DataTable dt = null;
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.SearchCount(keySearch, round, awardId);
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                strCount = dt.Rows[0][0].ToString();
            }
            int.TryParse(strCount, out count);
            pageNum = (int)Math.Ceiling((double)count / pageSize);
            if (pageNum * pageSize < count)
            {
                pageNum++;
            }
            return pageNum;
        }

        public static DataTable SearchXemBinhChon(string keySearch, string startDate, string endDate, int imageWidth, int imageHeight, int pageIndex, int pageSize, int pageType)
        {
            DataTable dt = null;
            if (dt == null)
            {
                dt = SearchXemBinhChonGetData(keySearch, startDate, endDate, imageWidth, imageHeight, pageIndex, pageSize, pageType);
            }

            return dt;
        }

        public static DataTable SearchXemBinhChonGetData(string keySearch, string startDate, string endDate, int imageWidth, int imageHeight, int pageIndex, int pageSize, int pageType)
        {
            DataTable dt = null;
            using (MainDB __db = new MainDB())
            {
                dt = __db.StoredProcedures.SearchXemBinhChon(keySearch, startDate, endDate, pageIndex, pageSize);
                if (dt != null && dt.Rows.Count > 0)
                {
                    dt = Utils.ReFormatTable(dt, imageWidth, imageHeight, pageType);
                }
            }
            return dt;
        }

        public static int SearchXemBinhChonPageNum(string keySearch, string startDate, string endDate, int pageSize)
        {
            int PageNum = 0;
            PageNum = SearchXemBinhChonPageNumGetData(keySearch, startDate, endDate, pageSize);

            return PageNum;
        }

        public static int SearchXemBinhChonPageNumGetData(string keySearch, string startDate, string endDate, int pageSize)
        {
            string strCount = "0";
            int pageNum;
            int count = 0;
            DataTable dt = null;
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.SearchXemBinhChonCount(keySearch, startDate, endDate);
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                strCount = dt.Rows[0][0].ToString();
            }
            int.TryParse(strCount, out count);
            pageNum = (int)Math.Ceiling((double)count / pageSize);
            if (pageNum * pageSize < count)
            {
                pageNum++;
            }
            return pageNum;
        }

        public static int UpdateVote(int ImageId)
        {
            int count = 0;
            DataTable dt = null;
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.UpdateVote(ImageId);
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                int.TryParse(dt.Rows[0][0].ToString(), out count); 
            }
            return count;
        }

        public static DataTable SearchXemBinhChon_NoDateTime(string keySearch, int imageWidth, int imageHeight, int pageIndex, int pageSize, int pageType)
        {
            DataTable dt = null;
            if (dt == null)
            {
                dt = SearchXemBinhChon_NoDateTime_GetData(keySearch,  imageWidth, imageHeight, pageIndex, pageSize, pageType);
            }

            return dt;
        }

        public static DataTable SearchXemBinhChon_NoDateTime_GetData(string keySearch, int imageWidth, int imageHeight, int pageIndex, int pageSize, int pageType)
        {
            DataTable dt = null;
            using (MainDB __db = new MainDB())
            {
                dt = __db.StoredProcedures.SearchXemBinhChon_NoDateTime(keySearch, pageIndex, pageSize);
                if (dt != null && dt.Rows.Count > 0)
                {
                    dt = Utils.ReFormatTable(dt, imageWidth, imageHeight, pageType);
                }
            }
            return dt;
        }

        public static int SearchXemBinhChon_NoDateTime_PageNum(string keySearch, int pageSize)
        {
            int PageNum = 0;
            PageNum = SearchXemBinhChon_NoDateTime_PageNumGetData(keySearch,pageSize);

            return PageNum;
        }

        public static int SearchXemBinhChon_NoDateTime_PageNumGetData(string keySearch, int pageSize)
        {
            string strCount = "0";
            int pageNum;
            int count = 0;
            DataTable dt = null;
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.SearchXemBinhChonCount_NoDateTime(keySearch);
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                strCount = dt.Rows[0][0].ToString();
            }
            int.TryParse(strCount, out count);
            pageNum = (int)Math.Ceiling((double)count / pageSize);
            if (pageNum * pageSize < count)
            {
                pageNum++;
            }
            return pageNum;
        } 

        public static int InsertImage(string acc, string Name,string Search, string Email, string Address, string Phone, string Other, string Img, string Title, string ShortContent, string LongContent, int RoundAward, string Birthday,string linkFB)
        {
            DataTable dt = null;
            int number = 0;
            using (MainDB __db = new MainDB())
            {
                dt = __db.StoredProcedures.InsertImage(acc, Name, Search, Email, Address, Phone, Other, Img, Title, ShortContent, LongContent, RoundAward, Birthday, linkFB);
            }
            if (Utils.TableIsNotNullOrEmpty(dt))
            {
                Int32.TryParse(dt.Rows[0][0].ToString(), out number);
            }
            return number;
        }
    }
}
